import 'package:distribution_mall_flutter/constant/status_constant.dart';
import 'package:distribution_mall_flutter/entity/order/order.dart';
import 'package:distribution_mall_flutter/entity/order/order_route_info_entity.dart';
import 'package:distribution_mall_flutter/entity/order/package/order_package_entity.dart';
import 'package:distribution_mall_flutter/http/modules/order.dart';
import 'package:distribution_mall_flutter/widgets/track_line.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

class OrderLogisticsState {
  OrderRouteInfoEntity? routeInfo;
  List<OrderPackageEntity> packageList;
  bool loading;

  OrderLogisticsState(
      {this.routeInfo, this.packageList = const [], this.loading = false});

  OrderLogisticsState.initial()
      : packageList = const [],
        loading = false;

  OrderLogisticsState copyWith(
          {OrderRouteInfoEntity? routeInfo,
          List<OrderPackageEntity>? packageList,
          bool? loading}) =>
      OrderLogisticsState(
        routeInfo: routeInfo ?? this.routeInfo,
        packageList: packageList ?? this.packageList,
        loading: loading ?? this.loading,
      );

  List<FormatTrckLineEntity> get formatRouteInfo {
    if (routeInfo == null || routeInfo?.routeInfoResult?.isEmpty == true) {
      return [];
    }

    List<FormatTrckLineEntity> list = [];
    routeInfo?.routeInfoResult?.forEach((item) {
      FormatTrckLineEntity trck = FormatTrckLineEntity();
      trck
        ..name = item.routeTitle
        ..des = item.routeDescription
        ..time = item.gmtOperation
        ..isChild = false;
      list.add(trck);
      item.details?.forEach((action) {
        FormatTrckLineEntity trck = FormatTrckLineEntity();
        trck
          ..name = action.event
          ..des = action.remark
          ..time = action.gmtOperation
          ..isChild = true;
        list.add(trck);
      });
    });
    return list;
  }

  List<Map<String, dynamic>> get formatPackageList {
    List<Map<String, dynamic>> list = [];
    if (packageList.isEmpty) return list;
    // 只拿第一个非关闭的订单包裹
    OrderPackageEntity item = packageList.firstWhere(
      (item) {
        return item.state != OrderStatusEnum.closed.status;
      },
      orElse: () => const OrderPackageEntity(),
    );
    // 取首位第一个包裹号
    if (item.firstShippingInfos?.isNotEmpty == true) {
      list.add({
        'number': item.firstShippingInfos?[0].logisticsTrackNumber ?? '',
        'name': item.firstShippingInfos?[0].logisticsCarrierCode ?? '',
      });
    }
    if (item.firstShippingInfos?.isNotEmpty == true) {
      list.add({
        'number': item.firstShippingInfos?[0].logisticsTrackNumber ?? '',
        'name': item.firstShippingInfos?[0].logisticsCarrierCode ?? '',
      });
    }
    return list;
  }
}

final orderLogisticsProvider = StateNotifierProvider.autoDispose<
    OrderLogisticsViewModal, OrderLogisticsState>((ref) {
  return OrderLogisticsViewModal();
});

class OrderLogisticsViewModal extends StateNotifier<OrderLogisticsState> {
  OrderLogisticsViewModal() : super(OrderLogisticsState.initial());

  routeInfoQuery(Map<String, dynamic> params) async {
    state = state.copyWith(loading: true);
    try {
      OrderRouteInfoEntity? data = await OrderApi.routeInfoQuery(params);
      if (data == null) return;
      state = state.copyWith(routeInfo: data);
    } finally {
      state = state.copyWith(loading: false);
    }
  }

  queryList(Map<String, dynamic> params) async {
    List<OrderPackageEntity>? data = await OrderApi.queryList(params);
    if (data == null) return;
    state = state.copyWith(packageList: data);
  }
}
